#import <MediaPlayer/MediaPlayer.h>
#import "RootViewController.h"

@implementation RootViewController

@synthesize lyricsTextView;
@synthesize managedObjectContext;
@synthesize currentTrackLyrics;
@synthesize currentTrackInfo;
@synthesize lyricsProvider;
@synthesize trackTitle;

- (void) mediaPicker:(MPMediaPickerController*)mediaPicker didPickMediaItems:(MPMediaItemCollection*)collection {
	[self dismissModalViewControllerAnimated:YES];
	
	MPMediaItem *item = [collection.items lastObject];
	TrackInfo *track = [[TrackInfo alloc] 
		initWithName: [item valueForProperty:MPMediaItemPropertyTitle]
		artist:		  [item valueForProperty:MPMediaItemPropertyArtist]
		id:			  [item valueForProperty:MPMediaItemPropertyPersistentID]
		lyrics:		  [item valueForProperty:MPMediaItemPropertyLyrics]
	];
	
	[self onCurrentTrackChanged:track];
	
	[track release];
}

- (void) mediaPickerDidCancel:(MPMediaPickerController*)mediaPicker {
	[self dismissModalViewControllerAnimated:YES];
}

- (IBAction) selectTrack:(id)sender {
	MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];
	
	picker.delegate = self;
	picker.prompt = @"Select song";
	
	[self presentModalViewController:picker animated:YES];
	[picker release];
}

- (TrackLyrics*) loadOrCreateTrackLyrics:(NSNumber*)id {
	NSError *error;
	TrackLyrics *trackLyrics;
	
	NSFetchRequest *request = [NSFetchRequest new];
	NSPredicate *predicate = [NSPredicate
		predicateWithFormat:@"id == %@",
		id
	];
	[request setPredicate:predicate];
	[request setEntity:
		[NSEntityDescription entityForName:@"TrackLyrics" inManagedObjectContext:managedObjectContext]
	];
	
	NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
	if (results == nil)
		NSLog(@"Track lyrics request failed");
	
	if ([results count] == 0) {
		trackLyrics = (TrackLyrics*)[NSEntityDescription 
			insertNewObjectForEntityForName:@"TrackLyrics" 
			inManagedObjectContext:managedObjectContext
		];
		
		trackLyrics.id = id;
		trackLyrics.lyrics = [NSString string];
	}
	else {
		trackLyrics = [results objectAtIndex:0];
	}
	
	[request release];
	
	return trackLyrics;
}

- (IBAction) fetchLyrics:(id)sender {
	NSString *lyrics = [lyricsProvider fetchLyricsForSong:currentTrackInfo];
	
	lyricsTextView.text = (lyrics != nil) ? lyrics : [NSString string];
}

- (IBAction) saveLyrics:(id)sender {
	if (currentTrackLyrics != nil)
		currentTrackLyrics.lyrics = lyricsTextView.text;
}

- (void) onCurrentTrackChanged:(TrackInfo*)track {
	if (track == nil)
		return;
	
	self.currentTrackInfo = track;
	self.currentTrackLyrics = [self loadOrCreateTrackLyrics:currentTrackInfo.id];
	lyricsTextView.text = currentTrackLyrics.lyrics;
	trackTitle.text = [NSString stringWithFormat:@"%@: %@", currentTrackInfo.artist, currentTrackInfo.name];
}

- (void) dealloc {
	[lyricsTextView release];
	[trackTitle release];
	[managedObjectContext release];
	[currentTrackLyrics release];
	[currentTrackInfo release];
	[lyricsProvider release];
	
	[super dealloc];
}

@end
